<?php

namespace app\index\controller;

use think\Controller;
use think\Request;

class Home extends Base
{
    // 个人信息
    public function index()
    {

        // 判断是否登陆
        if(!session("?index_user"))
        {
            $this->error("亲，请先登陆。",'index/login/index');
            die;
        }

        $title = '个人中心';

        return view('/home/home',[
            'title' => $title
        ]);
    }

    // 查看收获地址
    public function address()
    {
        // 判断是否登陆
        if(!session("?index_user"))
        {
            $this->error("亲，请先登陆。",'index/login/index');
            die;
        }

        $title = "修改收货地址";

        $user_id = session("index_user.id");

        $data = \app\index\model\Address::where("user_id",$user_id)->order('id','ASC')->select();

        return view("/home/address",[
            'title' => $title,
            'data' => $data
        ]);
    }

    // 添加地址
    public function insertss(Request $request)
    {
        // 判断用户当前是否登录，
        if(!session("?index_user")){
            $this->error("对不起您还没有登录，请先登录。",'/index/Login/index');
            die;
        }

        $data = $request->param();

        $rule = [
            'consignee|收货人' => "require",
            'address|地址' => "require",
            'phone|手机号' => "require|number",
        ];

        $validate = new \think\Validate($rule);

        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        // 判断状态值
        $data['check'] = !empty($data['check']) ? 1 : 0;
        // 判断是那个用户添加
        $data['user_id'] = session("index_user.id");

        \app\index\model\Address::create($data,true);

        $this->success("添加成功",'index/home/address');
    }

    // 删除收获地址
    public function deleteAdd(int $id)
    {
        if(empty($id))
        {
            $res = [
                "code" => 10001,
                'msg' => 'id错误'
            ];
            return json($res);
        }
        $user_id = session("index_user.id");

        \app\index\model\Address::where("user_id",$user_id)->delete($id);

        $res = [
            "code" => 10000,
            'msg' => 'success'
        ];

        return json($res);
    }
}
